package com.cc.spring.controller;

import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.cc.spring.common.CommonResp;
import com.cc.spring.utils.JWTUtil;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {
    @GetMapping("/token")
    public CommonResp<String> testToken(){
        CommonResp<String> commonResp = new CommonResp();
        HashMap<String, String> map = new HashMap<>();
        map.put("cc","18");
        String token = JWTUtil.createToken(map);
        commonResp.setContent(token);
        return  commonResp;
    }
    @GetMapping("/vertoken")
    public CommonResp<?> testTokenIsOK(HttpServletRequest request){
        CommonResp<DecodedJWT> commonResp = new CommonResp();
        String token = request.getHeader("token");
//        DecodedJWT decodedJWT = JWTUtil.verifyToken(token);
        DecodedJWT decodedJWT1 = JWTUtil.verifyToken(token);
        Map<String, Claim> claims = decodedJWT1.getClaims();
        System.out.println("cloaims"+ claims.get("cc"));
        commonResp.setContent(decodedJWT1);
        return commonResp;
    }
}
